import _extends from 'babel-runtime/helpers/extends';
import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
import _inherits from 'babel-runtime/helpers/inherits';
/**
 * 数据字典组件
 * @author: ljq
 * @since: 2018-10-26 11:55:09
 */
import * as React from 'react';
import request from '../_util/request';

var Dict = function (_React$Component) {
    _inherits(Dict, _React$Component);

    function Dict() {
        _classCallCheck(this, Dict);

        return _possibleConstructorReturn(this, (Dict.__proto__ || Object.getPrototypeOf(Dict)).apply(this, arguments));
    }

    return Dict;
}(React.Component);
// 配置


export default Dict;
Dict.url = '';
// 获取数据字典
Dict.getDicts = function (dictKeys) {
    var url = Dict.url;

    var dicts = {};
    // sessionStorage中有缓存直接取，没有就从服务器查询
    var needQueryDictKeys = [];
    dictKeys.forEach(function (dictkey) {
        var exist = sessionStorage['dict_' + dictkey];
        if (exist) {
            dicts[dictkey] = JSON.parse(exist);
        } else {
            needQueryDictKeys.push(dictkey);
        }
    });
    if (needQueryDictKeys.length > 0) {
        return request(url, {
            method: 'POST',
            body: { dictKeys: needQueryDictKeys }
        }).then(function (resp) {
            if (resp && resp.dicts) {
                dicts = _extends(_extends({}, dicts), resp.dicts);
                // 缓存
                for (var dictkey in resp.dicts) {
                    if (resp.dicts.hasOwnProperty(dictkey)) {
                        sessionStorage['dict_' + dictkey] = JSON.stringify(resp.dicts[dictkey]);
                    }
                }
            }
            return Promise.resolve(dicts);
        });
    } else {
        return Promise.resolve(dicts);
    }
};